from sqlalchemy import Column, String, Integer, BigInteger, TIMESTAMP, Text, ForeignKey, Boolean
from sqlalchemy.sql import func
from datetime import datetime
from .database import Base

class User(Base):
    """用户模型"""
    __tablename__ = "users"
    
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    email = Column(String(100), unique=True, index=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    is_active = Column(Boolean, default=True)
    created_at = Column(TIMESTAMP, nullable=True, server_default=func.now(), comment="用户注册时间")
    updated_at = Column(TIMESTAMP, nullable=True, server_default=func.now(), onupdate=func.now(), comment="信息更新时间")
    
class File(Base):
    """文件模型"""
    __tablename__ = "files"
    
    id = Column(BigInteger, primary_key=True, index=True, autoincrement=True, comment="文件唯一ID")
    user_id = Column(BigInteger, ForeignKey("users.id"), nullable=True, comment="上传用户的ID")
    file_name = Column(String(255), nullable=True, comment="文件原始名称")
    file_path = Column(String(500), nullable=True, comment="MinIO存储路径")
    file_type = Column(String(50), nullable=True, comment="文件类型（photo、video、audio、document）")
    file_size = Column(BigInteger, nullable=True, comment="文件大小（字节）")
    hash = Column(String(64), nullable=True, comment="文件哈希值（SHA256）用于去重")
    upload_time = Column(TIMESTAMP, nullable=True, server_default=func.now(), comment="文件上传时间")
    create_time = Column(TIMESTAMP, nullable=True, comment="文件创建时间（照片/视频拍摄时间）")
    extension = Column(String(20), nullable=True, comment="文件扩展名，如 jpg、mp4、pdf")
    mime_type = Column(String(100), nullable=True, comment="MIME 类型，如 image/jpeg、video/mp4")
    remark = Column(Text, nullable=True, comment="额外备注信息")
    is_deleted = Column(Boolean, nullable=False, default=False, server_default="0", comment="是否删除（0=正常，1=回收站）")
    directory_id = Column(BigInteger, nullable=False, default=0, server_default="0", comment="所属逻辑目录ID，0表示根目录")

class Directory(Base):
    """逻辑目录模型"""
    __tablename__ = "directories"
    
    id = Column(BigInteger, primary_key=True, index=True, autoincrement=True, comment="目录ID")
    name = Column(String(255), nullable=False, comment="目录名称")
    user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, comment="所属用户ID")
    parent_id = Column(BigInteger, nullable=False, default=0, server_default="0", comment="父目录ID，0表示根目录")
    path = Column(String(1000), nullable=True, comment="目录完整路径，如 /photos/2023/vacation")
    create_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), comment="创建时间")
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), comment="更新时间")
    is_deleted = Column(Boolean, nullable=False, default=False, server_default="0", comment="是否删除")

class UploadTask(Base):
    """上传任务模型，用于断点续传"""
    __tablename__ = "upload_tasks"
    
    id = Column(String(64), primary_key=True, index=True, comment="上传任务ID（UUID）")
    user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, comment="上传用户的ID")
    file_name = Column(String(255), nullable=False, comment="文件原始名称")
    file_size = Column(BigInteger, nullable=False, comment="文件总大小（字节）")
    chunk_size = Column(Integer, nullable=False, comment="分片大小（字节）")
    total_chunks = Column(Integer, nullable=False, comment="总分片数")
    uploaded_chunks = Column(Integer, nullable=False, default=0, comment="已上传分片数")
    file_type = Column(String(50), nullable=True, comment="文件类型（photo、video、audio、document）")
    mime_type = Column(String(100), nullable=True, comment="MIME 类型")
    status = Column(String(20), nullable=False, default="pending", comment="任务状态（pending、uploading、completed、failed）")
    upload_id = Column(String(255), nullable=True, comment="MinIO上传ID")
    create_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), comment="任务创建时间")
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), comment="任务更新时间")
    expires_at = Column(TIMESTAMP, nullable=True, comment="任务过期时间")
    directory_id = Column(BigInteger, nullable=False, default=0, server_default="0", comment="目标目录ID，0表示根目录")
    
class UploadChunk(Base):
    """上传分片模型，记录每个分片的上传状态"""
    __tablename__ = "upload_chunks"
    
    id = Column(BigInteger, primary_key=True, index=True, autoincrement=True, comment="分片ID")
    task_id = Column(String(64), ForeignKey("upload_tasks.id"), nullable=False, comment="上传任务ID")
    chunk_number = Column(Integer, nullable=False, comment="分片序号")
    chunk_size = Column(Integer, nullable=False, comment="分片大小（字节）")
    etag = Column(String(100), nullable=True, comment="分片的ETag")
    status = Column(String(20), nullable=False, default="pending", comment="分片状态（pending、uploaded）")
    upload_time = Column(TIMESTAMP, nullable=True, comment="分片上传时间")

class BackupTask(Base):
    """备份任务模型"""
    __tablename__ = "backup_tasks"
    
    id = Column(BigInteger, primary_key=True, index=True, autoincrement=True, comment="备份任务ID")
    user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, comment="用户ID")
    task_name = Column(String(255), nullable=False, comment="任务名称")
    backup_path = Column(String(500), nullable=False, comment="备份存储路径")
    status = Column(String(20), nullable=False, default="pending", comment="任务状态(pending、running、completed、failed)")
    last_backup_time = Column(TIMESTAMP, nullable=True, comment="上次备份时间")
    schedule_type = Column(String(20), nullable=False, default="manual", comment="计划类型(manual、daily、weekly、monthly)")
    is_incremental = Column(Boolean, nullable=False, default=True, server_default="1", comment="是否为增量备份")
    created_at = Column(TIMESTAMP, nullable=False, server_default=func.now(), comment="创建时间")
    updated_at = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), comment="更新时间")
    total_file_count = Column(Integer, default=0, comment="备份文件总数")
    total_file_size = Column(BigInteger, default=0, comment="备份文件总大小(字节)")
    source_directory_ids = Column(String(1000), nullable=True, comment="源目录ID列表，JSON格式")
    
class BackupFile(Base):
    """备份文件记录模型"""
    __tablename__ = "backup_files"
    
    id = Column(BigInteger, primary_key=True, index=True, autoincrement=True, comment="记录ID")
    backup_task_id = Column(BigInteger, ForeignKey("backup_tasks.id"), nullable=False, comment="所属备份任务ID")
    file_id = Column(BigInteger, ForeignKey("files.id"), nullable=False, comment="原始文件ID")
    backup_path = Column(String(500), nullable=False, comment="备份存储路径")
    backup_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), comment="备份时间")
    file_size = Column(BigInteger, nullable=False, comment="文件大小(字节)")
    is_latest = Column(Boolean, nullable=False, default=True, server_default="1", comment="是否为最新备份")
    backup_type = Column(String(20), nullable=False, default="full", comment="备份类型(full、incremental)")
    status = Column(String(20), nullable=False, default="completed", comment="状态(pending、completed、failed)") 